import React from 'react';
import {Spin, Card, Button, Table, Row, Col} from 'antd';
import DescriptionList from 'ant-design-pro/lib/DescriptionList';
import PropTypes from 'prop-types';
import {transportType, pickingListStatus, pickingType, currency} from '../Enum';
import routes from './routes';
import {TABLE} from '../constants';
import {conventEnumValueToString, formatAmount} from '../utils';
import {getDetail} from './api';
import {injectIntl} from './intl';
const {Description} = DescriptionList;
class DetailPanel extends React.PureComponent {
    state = {
        isFetching: false,
        data: {},
        details: []
    }
    componentDidMount() {
        this.getDetailInfo();
    }
    getDetailInfo = () => {
        const id = this.props.id;
        this.setState({isFetching: true});
        getDetail(id)
            .then(res => {
                if(res.ok)
                    this.setState({
                        isFetching: false,
                        data: res.data,
                        details: res.data.details
                    });
                else
                    this.setState(({
                        isFetching: false
                    }));
            });
    }
    onCancel = () => {
        this.props.history.push(routes.query.url());
    }
    render() {
        const {data} = this.state;
        const {formatMessage} = this.props.intl;
        const colums = [{
            title: formatMessage({
                id: 'detailPanel.column.partCode',
                defaultMessage: '备件编号'
            }),
            dataIndex: 'partCode',
        }, {
            title: formatMessage({
                id: 'detailPanel.column.partName',
                defaultMessage: '备件名称'
            }),
            dataIndex: 'partName',
        }, {
            title: formatMessage({
                id: 'detailPanel.column.oldPartCode',
                defaultMessage: '被替代件编号'
            }),
            dataIndex: 'oldPartCode',
        }, {
            title: formatMessage({
                id: 'detailPanel.column.oldPartName',
                defaultMessage: '被替代件名称'
            }),
            dataIndex: 'oldPartName',
        }, {
            title: formatMessage({
                id: 'detailPanel.column.pickingQuantity',
                defaultMessage: '交货数量'
            }),
            dataIndex: 'pickingQuantity'
        }, {
            title: formatMessage({
                id: 'detailPanel.column.packedQuantity',
                defaultMessage: '装箱数量'
            }),
            dataIndex: 'packedQuantity'
        }, {
            title: formatMessage({
                id: 'detailPanel.column.price',
                defaultMessage: '单价'
            }),
            dataIndex: 'price',
            render: text => formatAmount(text)
        }, {
            title: formatMessage({
                id: 'detailPanel.column.pickedTotalAmount',
                defaultMessage: '交货金额'
            }),
            dataIndex: 'pickedTotalAmount',
            render: text => formatAmount(text)
        }, {
            title: formatMessage({
                id: 'detailPanel.column.packedTotalAmount',
                defaultMessage: '装箱金额'
            }),
            dataIndex: 'packedTotalAmount',
            render: text => formatAmount(text)
        }];
        return (
            <div>
                <Spin spinning={this.state.isFetching}>
                    <Card>
                        <DescriptionList title={<Row><Col span={6}>{
                            formatMessage({
                                id: 'detailPanel.card.picking',
                                defaultMessage: '订单信息'
                            })
                        }</Col>
                        </Row>}>
                            <Description term={formatMessage({
                                id: 'detailPanel.operation.code',
                                defaultMessage: '编号'
                            })}>{data.code}
                            </Description>
                            <Description term={formatMessage({
                                id: 'detail.operation.dealerCode',
                                defaultMessage: '总代编号'
                            })}>{data.dealerCode}
                            </Description>
                            <Description term={formatMessage({
                                id: 'detail.operation.dealerName',
                                defaultMessage: '总代名称'
                            })}>{data.dealerName}
                            </Description>
                            <Description term={formatMessage({
                                id: 'detailPanel.operation.partOrderCode',
                                defaultMessage: '备件订单编号'
                            })}>{data.partOrderCode}
                            </Description>
                            <Description term={formatMessage({
                                id: 'detail.operation.brandCode',
                                defaultMessage: '品牌'
                            })}>{data.brandCode}
                            </Description>
                            <Description term={formatMessage({
                                id: 'detail.operation.pickingType',
                                defaultMessage: '拣货类型'
                            })}>{conventEnumValueToString(pickingType, data.pickingType)}
                            </Description>
                            <Description term={formatMessage({
                                id: 'detailPanel.operation.status',
                                defaultMessage: '状态'
                            })}>{conventEnumValueToString(pickingListStatus, data.status)}
                            </Description>
                            <Description term={formatMessage({
                                id: 'detailPanel.operation.currency',
                                defaultMessage: '币种'
                            })}>{conventEnumValueToString(currency, data.currency)}
                            </Description>
                            <Description term={formatMessage({
                                id: 'detailPanel.operation.partOrderTypeName',
                                defaultMessage: '订单类型'
                            })}>{data.partOrderTypeName}</Description>
                            <Description term={formatMessage({
                                id: 'detailPanel.operation.transportType',
                                defaultMessage: '运输方式'
                            })}>{conventEnumValueToString(transportType, data.transportType)}</Description>
                            <Description term={formatMessage({
                                id: 'detailPanel.operation.transferOrderCode',
                                defaultMessage: '调拨单号'
                            })}>{data.transferOrderCode}
                            </Description>
                            <Description term={formatMessage({
                                id: 'detailPanel.operation.deliveryCode',
                                defaultMessage: '交货单号'
                            })}>{data.deliveryCode}
                            </Description>
                            <Description term={formatMessage({
                                id: 'detailPanel.operation.itemCount',
                                defaultMessage: '品种数'
                            })}>{data.itemCount}
                            </Description>
                            <Description term={formatMessage({
                                id: 'detailPanel.operation.pickingQuantityCount',
                                defaultMessage: '交货总数量'
                            })}>{data.pickingQuantityCount}
                            </Description>
                            <Description term={formatMessage({
                                id: 'detailPanel.operation.pickedTotalAmountFee',
                                defaultMessage: '交货金额'
                            })}>{formatAmount(data.pickedTotalAmountFee)}
                            </Description>
                        </DescriptionList>
                    </Card>
                    <Card>
                        <DescriptionList title={formatMessage({
                            id: 'detailPanel.table.details',
                            defaultMessage: '清单'
                        })}>
                            <Table
                                className="white-space-nowrap"
                                columns={colums}
                                rowKey="partId"
                                pagination={false}
                                dataSource={this.state.details}
                                {...TABLE} />
                        </DescriptionList>
                    </Card>
                </Spin>
                <div className="page-toolbar">
                    <Button type="primary" onClick={this.onCancel}>{this.props.intl.formatMessage({
                        id: 'detailPanel.operation.onCancel',
                        defaultMessage: '返回'
                    })}</Button>
                </div>
            </div>
        );
    }
}

DetailPanel.propTypes = {
    history: PropTypes.object,
    id: PropTypes.string,
    intl: PropTypes.object
};

import {connect} from 'react-redux';
const mapDispatchToProps = (dispatch, props) => ({
    onCancel: () => {
        props.history.push(routes.query.url());
    },
});
export default connect(null, mapDispatchToProps)(injectIntl(DetailPanel));
